package com.qbw.app.module.vehicle.holder;

import android.content.Context;
import android.graphics.Color;
import android.view.ViewGroup;

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.qbw.app.R;
import com.qbw.app.databinding.VehicleTarvekChartBinding;
import com.qbw.app.model.TravelChart;
import com.qbw.app.module.common.holder.CommonHolder;

import java.util.ArrayList;

/**
 * @author QBW
 * @date 2019-09-02
 */
public class VehicleTravekCharHolder extends CommonHolder<TravelChart.DataList, VehicleTarvekChartBinding> {

    private String dateMonth;
    public VehicleTravekCharHolder(Context context, ViewGroup viewParent) {
        super(context, R.layout.vehicle_tarvek_chart, viewParent);
    }


    @Override
    public void bindData(int position, TravelChart.DataList data) {
        super.bindData(position, data);
        mBinding.executePendingBindings();
        dateMonth = data.getList().get(0).getCurrentMonth();
        setBarChartView();

    }


    private void setBarChartView() {
        mBinding.chart.getDescription().setEnabled(false);
        mBinding.chart.setDrawGridBackground(false);

        XAxis xAxis =  mBinding.chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if(value == 0){
                    return dateMonth+" - 1";
                }else if(value == 5){
                    return dateMonth+" - 5";
                }else if(value == 10){
                    return dateMonth+" - 10";
                }else if(value == 15){
                    return dateMonth+" - 15";
                }else if(value == 20){
                    return dateMonth+" - 20";
                }else if(value == 25){
                    return dateMonth+" - 25";
                }
                return "";
            }
        });
        YAxis leftAxis = mBinding.chart.getAxisLeft();
        leftAxis.setLabelCount(5, false);
        leftAxis.setSpaceTop(15f);
        leftAxis.setAxisMaximum(150f);  // 设置y轴的最大值
        leftAxis.setAxisMinimum(0f);  // 设置y轴的最小值
        YAxis rightAxis =mBinding.chart.getAxisRight();
        rightAxis.setLabelCount(5, false);
        rightAxis.setSpaceTop(15f);
        rightAxis.setAxisMaximum(150f);  // 设置y轴的最大值
        rightAxis.setAxisMinimum(0f);  // 设置y轴的最小值
        mBinding.chart.setData(generateData(1));
        mBinding.chart.setFitBars(true);
        mBinding.chart.animateY(700);
    }

    private BarData generateData(int cnt) {

        ArrayList<BarEntry> entries = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            if (i == 4) {
                entries.add(new BarEntry(i, 78));
            } else if (i == 6) {
                entries.add(new BarEntry(i, 28));
            } else if (i == 11) {
                entries.add(new BarEntry(i, 55));
            } else if (i == 14) {
                entries.add(new BarEntry(i, 78));
            } else if (i == 23) {
                entries.add(new BarEntry(i, 128));
            } else {
                entries.add(new BarEntry(i, 0));
            }
        }
        BarDataSet d = new BarDataSet(entries, "");
        d.setColors(ColorTemplate.TRIP_VORDIPLOM_COLORS);
        d.setBarShadowColor(Color.rgb(144, 152, 164));
        ArrayList<IBarDataSet> sets = new ArrayList<>();
        sets.add(d);

        BarData cd = new BarData(sets);
        cd.setBarWidth(0.9f);
        return cd;
    }


    @Override
    protected void onClickItem() {
        super.onClickItem();
    }
}
